package org.wang.generalcode.modules.gen.util;

import oracle.jdbc.OracleConnection;
import org.wang.generalcode.modules.gen.config.GenDataSourceConfig;
import org.wang.generalcode.modules.gen.pojo.enums.DbType;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


/**
 * DB工具类
 */
public class DbUtils {
    private static final int CONNECTION_TIMEOUTS_SECONDS = 6;

    /**
     * 获得数据库连接
     */
    public static Connection getConnection(GenDataSourceConfig dataSourceConfig) throws ClassNotFoundException, SQLException {
        DriverManager.setLoginTimeout(CONNECTION_TIMEOUTS_SECONDS);
        Class.forName(dataSourceConfig.getDbType().getDriverClass());

        Connection connection = DriverManager.getConnection(dataSourceConfig.getConnUrl(), dataSourceConfig.getUsername(), dataSourceConfig.getPassword());
        if (dataSourceConfig.getDbType() == DbType.Oracle) {
            ((OracleConnection) connection).setRemarksReporting(true);
        }

        return connection;
    }

}
